#!/bin/bash

TIMEOUT_COLORBAR=5
WAIT_TIME_FOR_FVT=2
RESULT_BT=0
RESULT_WIFI=0
RESULT_ETH=0
RESULT_USB=0
RESULT_DIP=0
RESULT_TS_LT=0
RESULT_TS_LB=0
RESULT_TS_RT=0
RESULT_TS_RB=0
RESULT_TS_CT=0 
ETH_ADDRESS="192.168.1."
ETH_PING_ADDRESS=${ETH_ADDRESS}1
WIFI_AP_SSID="welbilt_ap"
WIFI_AP_ADDRESS="192.168.2.1"
DIP_STATUS="0000host"


# Functions
# =========
Show_Result()
{
	echo "****************"
	echo " BT        "$([ $RESULT_BT -eq 1 ]&&echo "PASS"||echo "*FAIL")
	echo " Ethernet  "$([ $RESULT_ETH -eq 1 ]&&echo "PASS"||echo "*FAIL")
	echo " Wi-Fi     "$([ $RESULT_WIFI -eq 1 ]&&echo "PASS"||echo "*FAIL")
	echo " USB       "$([ $RESULT_USB -eq 1 ]&&echo "PASS"||echo "*FAIL")
	echo " DIP       "$([ $RESULT_DIP -eq 1 ]&&echo "PASS"||echo "*FAIL")
        echo " Touch-LT  "$([ $RESULT_TS_LT -eq 1 ]&&echo "PASS"||echo "*FAIL")
        echo " Touch-LB  "$([ $RESULT_TS_LB -eq 1 ]&&echo "PASS"||echo "*FAIL")
        echo " Touch-RT  "$([ $RESULT_TS_RT -eq 1 ]&&echo "PASS"||echo "*FAIL")
        echo " Touch-RB  "$([ $RESULT_TS_RB -eq 1 ]&&echo "PASS"||echo "*FAIL")
        echo " Touch-CT  "$([ $RESULT_TS_CT -eq 1 ]&&echo "PASS"||echo "*FAIL")
	echo "****************"
}


Check_Bt()
{
	echo "BT testing..."
	if [ $(/home/root/jabil/bt_scan|grep PASS|wc -l) -eq 1 ]; then
		RESULT_BT=1 
		echo 1 > /home/root/jabil/RESULT_BT&&sync
	else
		echo 0 > /home/root/jabil/RESULT_BT&&sync                                    
	fi           
}


Check_Eth()
{
	#LAST_ADDR=$(shuf -i 15-254 -n 1)
	#echo "Ethernet testing...(Unit IP:${ETH_ADDRESS}$LAST_ADDR, ping $ETH_PING_ADDRESS)"
	#ifconfig eth0 ${ETH_ADDRESS}$LAST_ADDR up
	#sleep 5
	sleep 1
	if [ $(ping -c 1 $ETH_PING_ADDRESS|grep ', 0% packet loss'|wc -l) -eq 1 ]; then
		RESULT_ETH=1
		echo 1 > /home/root/jabil/RESULT_ETH&&sync
	else
		echo 0 > /home/root/jabil/RESULT_ETH&&sync
	fi
}


Check_Wifi() 
{                                                                            
        echo "Wi-Fi testing...(AP SSID:$WIFI_AP_SSID, AP IP:$WIFI_AP_ADDRESS)"
#        ifconfig wlan0 up 
#	sleep 3
#	iw dev wlan0 connect $WIFI_AP_SSID
#	sleep 5
#	if [ $(iwconfig wlan0|grep 'ESSID:off/any'|wc -l) -eq 1 ]; then
#		iw dev wlan0 connect $WIFI_AP_SSID
#		sleep 5
#	fi
#	udhcpc -t 5 -n -i wlan0                                  
#       systemctl start wpa_supplicant@wlan0
	ifconfig wlan0 down&&ifconfig wlan0 up&&sleep 5                                                  
        if [ $(iwconfig wlan0|grep 'ESSID:off/any'|wc -l) -eq 1 ]; then                       
                echo 0 > /home/root/jabil/RESULT_WIFI                                         
        else                                                                                  
                echo "AP is connected"
                if [ $(ping -c 1 $WIFI_AP_ADDRESS|grep ', 0% packet loss'|wc -l) -eq 1 ]; then
                        RESULT_WIFI=1                                                         
                        echo 1 > /home/root/jabil/RESULT_WIFI&&sync                                 
                else                                                                          
                        echo 0 > /home/root/jabil/RESULT_WIFI&&sync                                 
                fi                                                                            
        fi                                                                      
}


Check_Usb()
{                                                                              
        echo "USB testing..."
        if [ $(df|grep '/dev/sda1'|wc -l) -eq 1 ]; then
		if [ $(df|grep '/dev/sdb1'|wc -l) -eq 1 ]; then
                	RESULT_USB=1
			echo 1 > /home/root/jabil/RESULT_USB&&sync  
		else
			echo 0 > /home/root/jabil/RESULT_USB&&sync
			echo "One of USB fail"
		fi
	else
		echo 0 > /home/root/jabil/RESULT_USB&&sync                                   
        fi                      
} 


Check_Dip()
{                                                                               
        echo "DIP swithc test..."
        if [ $(/home/root/jabil/dip_chk) == $DIP_STATUS ]; then                 
                RESULT_DIP=1
		echo 1 > /home/root/jabil/RESULT_DIP&&sync
	else
		echo 0 > /home/root/jabil/RESULT_DIP&&sync
        fi                      
}

Check_Touch()
{                                                                               
        echo "Touch test..."
                                                                                
        echo "Left-top"
        res=$(/home/root/jabil/tsTest 0 0 100)                                  
        tmp=$(echo $res|awk -F' ' '{print $NF}')                                
        if [ "$tmp" == "TsTest:PASS" ]; then                                    
                RESULT_TS_LT=1                                                  
        fi                                                                      
                                                                                
        echo "Left-bottom"
        res=$(/home/root/jabil/tsTest 0 500 100)                                
        tmp=$(echo $res|awk -F' ' '{print $NF}')                                
        if [ "$tmp" == "TsTest:PASS" ]; then                                    
                RESULT_TS_LB=1                                                  
        fi                                                                      
                                                                                
        echo "Right-top"
        res=$(/home/root/jabil/tsTest 924 0 100)                                
        tmp=$(echo $res|awk -F' ' '{print $NF}')                                
        if [ "$tmp" == "TsTest:PASS" ]; then                                    
                RESULT_TS_RT=1                                                  
        fi                                                                      
                                                                                
        echo "Right-bottom"
        res=$(/home/root/jabil/tsTest 924 500 100)                              
        tmp=$(echo $res|awk -F' ' '{print $NF}')                                
        if [ "$tmp" == "TsTest:PASS" ]; then                                    
                RESULT_TS_RB=1                                                  
        fi                                                                      
                                                                                
        echo "Central"
        res=$(/home/root/jabil/tsTest 462 250 100)                              
        tmp=$(echo $res|awk -F' ' '{print $NF}')                                
        if [ "$tmp" == "TsTest:PASS" ]; then                                    
                RESULT_TS_CT=1                                                  
        fi

        echo "$RESULT_TS_LT$RESULT_TS_LB$RESULT_TS_RT$RESULT_TS_RB$RESULT_TS_CT" > /home/root/jabil/RESULT_TS&&sync               
} 


# Script start point
# ==================
console=$(tty)
echo "console=$console"
if [ "$console" == "/dev/tty1" ]; then
	sleep $WAIT_TIME_FOR_FVT
	if [ $(cat /home/root/jabil/FVT.status) -eq 1 ]; then
		echo "=== Assembly test start ==="
		echo "DSP_VER: $(cat /home/root/jabil/VERSION)"
		echo "MFG_TOOL_VER: $(cat /home/root/jabil/MFG_VERSION)"
		echo "Check SN"
		#sn=$(fdtget -t s /home/root/jabil/welbilt-factory.bin /features device-serial)
		sn=$(/home/root/jabil/getSN)
		if [ "$sn" == "" ]; then
			echo "SN is empty,  Don't run assembly test."
		else
			echo "Start Test...SN:$sn"
			echo "Enable Networking..."
			systemctl start wpa_supplicant@wlan0
			echo "Ethnet IP Address: 192.168.2.10"
			echo "Wi-Fi IP Address: 192.168.2.10"
			
                        # check BT
                        Check_Bt
			if [ $RESULT_BT == "0" ]; then
				Check_Bt                                      
			fi
			
			# check Ethernet
			Check_Eth

			# check WiFi
			Check_Wifi

			# check USB			
			Check_Usb

                        # check DIP                                             
                        Check_Dip                

                        # output sound                                          
                        echo "Play a sound..."
                        #aplay /home/root/jabil/Front_Center.wav > /dev/null  
			aplay /home/root/jabil/1kTone.wav > /dev/null   
                                                                                
                        # touch test                                            
                        #ts_test > /dev/null
			Check_Touch                                                 
                                                                                
                        # show a colorbar                                       
                        echo "Show a colorbar..."
                        /home/root/jabil/colorbarEx &                           
                        sleep $TIMEOUT_COLORBAR                                 
                        killall -2 colorbarEx                                   
                                                                                
                        echo " "
                        echo "=== Assembly test summary ==="
                        Show_Result                                             
                        echo "You should:"
                        echo " - hear sound out"
                        #echo " - sweep the screen and a cross cursor follow your figner"
                        echo " - see a colorbard shown $TIMEOUT_COLORBAR secs"
			echo "=== Assembly test end ==="
		fi
	else
		echo "FVT not PASS. Don't run assembly test."
	fi
else
	echo "Do nothing"
fi
